Redis docker 主从模式与哨兵sentinel |
您所在的位置:网站首页 › docker redis sentinel › Redis docker 主从模式与哨兵sentinel |
为实现redis的高可用,我们采用主从模式加哨兵的方法。 一主二从三哨兵,共启动6个redis容器。本文示例在同一个服务器上进行操作。 开发环境centos 假设ip地址为 x.x.x.1docker 1.13.1redis 7.0.2系统为centos cat /proc/versionLinux version 3.10.0-693.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017后面的操作将在root权限下进行 docker版本docker -v Docker version 1.13.1, build 7d71120/1.13.1redis版本我们使用7.0.2版本。用到的服务器上的redis最好统一版本。 docker pull redis:7.0.2不同版本的redis的配置不一定相同。如果启动容器出现一直在restarting的情况,去看一下log 查看已经启动的redis容器中的redis版本 docker exec -it [容器id] redis-server -vRedis server v=7.0.2 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=40f017f9608e455e来官网找对应的安装包 http://download.redis.io/releases/ 解压后可以得到redis.conf和sentinel.conf文件 主从结构一个主redis,2个从redis。它们使用不同的3个端口,注意检查防火墙的设置。 本文假设服务器的ip为x.x.x.1。 启动主redis主redis,即master。 启动主redis容器 docker run --restart=always -p 6400:6379 --name redis-CNT-MASTER \-d redis:7.0.2 redis-server --requirepass 778899 --masterauth 7788996400:6379 指定服务器的6400对应redis容器里的6379端口--requirepass 778899 设定密码--masterauth 778899 从redis连上来需要的密码进入容器查看状态 docker exec -it redis-CNT-MASTER redis-cli127.0.0.1:6379> auth 778899OK127.0.0.1:6379> info replication启动1号从redis从redis使用配置的方式 文件结构 /home/dapp/projects/rustfisher/redis-slave1 ├── data1号从redis的redis.conf除掉注释后的部分 #bind 0.0.0.0 # 不用bindslaveof x.x.x.1 6400 # 记得改成你的服务器ipreplica-announce-ip x.x.x.1 # 记得改成你的服务器ipreplica-announce-port 6401 # 从redis对外的端口,后面启动的时候也要配置的protected-mode noport 6379masterauth 778899requirepass 778899 tcp-backlog 511timeout 0tcp-keepalive 300daemonize nopidfile /var/run/redis_6379.pidloglevel noticelogfile ""databases 16always-show-logo noset-proc-title yesproc-title-template "{title} {listen-addr} {server-mode}"stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbrdb-del-sync-files nodir ./replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync yesrepl-diskless-sync-delay 5repl-diskless-sync-max-replicas 0repl-diskless-load disabledrepl-disable-tcp-nodelay noreplica-priority 100acllog-max-len 128lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush nolazyfree-lazy-user-del nolazyfree-lazy-user-flush nooom-score-adj nooom-score-adj-values 0 200 800disable-thp yesappendonly yesappendfilename "appendonly.aof"appenddirname "appendonlydir"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yesaof-timestamp-enabled noslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-listpack-entries 512hash-max-listpack-value 64list-max-listpack-size -2list-compress-depth 0set-max-intset-entries 512zset-max-listpack-entries 128zset-max-listpack-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesjemalloc-bg-thread yes 启动1号从redis docker run --restart=always -p 6401:6379 --name redis-CNT-S1 \-v /home/dapp/projects/rustfisher/redis-slave1/redis.conf:/etc/redis/redis.conf \-v /home/dapp/projects/rustfisher/redis-slave1/data:/data \-d redis:7.0.2 redis-server /etc/redis/redis.conf6401:6379 1号从redis对外端口是6401redis-CNT-S1 容器名字启动2号从redis配置文件可以复制1号的。然后记得修改它的宣称ip和端口 replica-announce-ip x.x.x.1replica-announce-port 6402 # 2号用的端口启动2号从redis,需要注意对应的路径 docker run --restart=always -p 6402:6379 --name redis-CNT-S2 \-v /home/dapp/projects/rustfisher/redis-slave2/redis.conf:/etc/redis/redis.conf \-v /home/dapp/projects/rustfisher/redis-slave2/data:/data \-d redis:latest redis-server /etc/redis/redis.conf查看主从信息info replication进入主redis容器,输入redis-cli,auth后,检查情况info replication 例如进入主redis容器查看。此时connected_slaves:2。slave的ip和port应该和它们宣称replica-announce的一致。 在主redis中set a 123,在1号和2号从redis里get a可以看到效果。 哨兵 sentinel我们会启动3个新的redis容器,即3个哨兵。这3个哨兵都监听主redis。 哨兵1号新建一个哨兵1号用的目录 /home/dapp/projects/rustfisher/sentinel1 ├── conf│ └── sentinel.conf└── data先编辑配置文件 sentinel.conf port 6411dir "/tmp"sentinel monitor master001 x.x.x.1 6400 2 # 记得修改成你的主redis的ip和端口sentinel auth-pass master001 778899 # 密码是前面定的sentinel down-after-milliseconds master001 30000sentinel parallel-syncs master001 1sentinel failover-timeout master001 180000sentinel deny-scripts-reconfig yesport 6411 指定的是哨兵容器里自己的端口sentinel monitor 指定了要监听的主master的ip和端口,最后那个2表示需要2个哨兵投票master001 是我们给主redis起的名字,后面都用这个启动1号哨兵 docker run --restart=always -p 6411:6411 --name redis-sentinel-CNT-1 --privileged=true \-v /home/dapp/projects/rustfisher/sentinel1/conf:/usr/local/etc/redis/conf/ \-d redis:7.0.2 redis-sentinel /usr/local/etc/redis/conf/sentinel.conf注意:这里需要映射的是目录。-v目录对目录。 进入容器后,可以查看相关信息。redis-cli需要指定端口-p 6411 root@aa8d208546d1:/data# redis-cli -p 6411127.0.0.1:6411> sentinel master master001查看服务器上1号哨兵的 sentinel.conf ,发现多了一些内容,是redis哨兵写进来的 # Generated by CONFIG REWRITElatency-tracking-info-percentiles 50 99 99.9user default on nopass ~* &* +@allsentinel myid b43e361ff80b8f9106cb1d4bb59421aa909ac370sentinel config-epoch master001 0sentinel leader-epoch master001 0sentinel current-epoch 0sentinel known-replica master001 x.x.x.1 6401 sentinel known-replica master001 x.x.x.1 6402 启动2号哨兵配置2号的路径/home/dapp/projects/rustfisher/sentinel2。 sentinel.conf配置内容和前面一样,启动时候端口用-p 6412:6411 docker run --restart=always -p 6412:6411 --name redis-sentinel-CNT-2 --privileged=true \-v /home/dapp/projects/rustfisher/sentinel2/conf:/usr/local/etc/redis/conf/ \-d redis:7.0.2 redis-sentinel /usr/local/etc/redis/conf/sentinel.conf启动3号哨兵同理,路径用3号自己的 /home/dapp/projects/rustfisher/sentinel3/conf 端口-p 6413:6411 docker run --restart=always -p 6413:6411 --name redis-sentinel-CNT-3 --privileged=true \-v /home/dapp/projects/rustfisher/sentinel3/conf:/usr/local/etc/redis/conf/ \-d redis:7.0.2 redis-sentinel /usr/local/etc/redis/conf/sentinel.conf查看哨兵情况进入哨兵redis容器查看情况 root@5dc0468fb71f:/data# redis-cli -p 6411127.0.0.1:6411> sentinel master master001num-slaves 表示从redis的数量num-other-sentinels 表示除自己外的哨兵数量哨兵测试停掉主redis容器 docker stop [id]例如 docker stop redis-CNT-MASTER过一会等选出新的主redis。然后再启动刚才停掉的容器redis-CNT-MASTER。查看信息,发现它是role:slave [root@rustfisher_test01 redis-slave1]# docker exec -it redis-CNT-MASTER redis-cli -a 778899 info replicationWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:slavemaster_host:x.x.x.1master_port:6402master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_read_repl_offset:886622slave_repl_offset:886622slave_priority:100slave_read_only:1replica_announced:1connected_slaves:0master_failover_state:no-failovermaster_replid:6e8a8cb6c167abeb743e668b654e2f470a537742master_replid2:0000000000000000000000000000000000000000master_repl_offset:886622second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:409622repl_backlog_histlen:477001从上面看出,现在的主redis变成x.x.x.1:6402了。 哨兵常见问题配置文件映射指定配置文件sentinel.conf映射到容器内时,直接了使用文件映射。这么做有可能导致哨兵没有写入配置文件的权限, 查看log会看到: WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy. 解决方法是使用目录映射。像上面那样:-v /home/dapp/projects/rustfisher/sentinel2/conf:/usr/local/etc/redis/conf/ 哨兵myid主从与哨兵redis都启动后,看起来OK了。但stop掉主redis后,哨兵并没有选出新的主redis。 有一种可能是哨兵改写的sentinel.conf里使用了相同的myid。 grep -nr myidsentinel1/conf/sentinel.conf:11:sentinel myid b43e361ff80b8f9106cb1d4bb59421aa909ac370sentinel2/conf/sentinel.conf:11:sentinel myid e19342addbcdd8d034c1e91ed74ff94a7aec2e2asentinel3/conf/sentinel.conf:11:sentinel myid d0393d72f69556f2047cf8c84cfa20f4df6ed4ff解决方法是stop掉那个哨兵,删掉myid那行,然后重启哨兵。它会自动生成新的myid。 参考Redis官网文档 redis.io |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |